#import "HHTagCollectionView.h"
#import "HHTagCollectionViewFlowLayout.h"
#import "HHTagCollectionCell.h"
#import <BCFoundation/UIImage+BCHelper.h>
#import <BCFoundation/NSString+BCHelper.h>

@interface HHTagCollectionView ()
<
UICollectionViewDelegate,
UICollectionViewDataSource
>

@property (nonatomic,strong) HHTagCollectionViewFlowLayout *layout;
@property (nonatomic,strong) UICollectionView *collectionView;
@property (nonatomic,strong) NSArray *tagSizeArr;

@end

@implementation HHTagCollectionView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self configSubViews];
    }
    return self;
}

///MARK: - setter
- (void)setConfig:(HHTagListConfig * _Nonnull)config {
    _config = config;
    _layout.sectionInset = config.contentPadding;
    _layout.minimumLineSpacing = config.lineSpacing;
    _layout.minimumInteritemSpacing = config.rowSpacing;
    
    
}
- (void)setTags:(NSArray<id<HHTagItemProtocol>> *)tags {
    _tags = tags;
    NSMutableArray *mArr = [[NSMutableArray alloc] init];
    for (id<HHTagItemProtocol> tagItem in tags) {
        CGSize tagSize = [self.class intrinsicContentSize:tagItem.name withConfig:self.config];
        [mArr addObject:[NSValue valueWithCGSize:tagSize]];
    }
    self.tagSizeArr = mArr;
    [self.collectionView reloadData];
}


///MARK: - UICollectionViewDelegate UICollectionViewDataSource

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.tags.count;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    NSValue *sizeVal = self.tagSizeArr[indexPath.item];
    return sizeVal.CGSizeValue;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    HHTagCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass(HHTagCollectionCell.class) forIndexPath:indexPath];
    return  cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {

}
#pragma mark - Public method
- (void)reloadData {
    [self.collectionView reloadData];
}

#pragma mark - Subviews
- (void)configSubViews{
    [self addSubview:self.collectionView];
}

#pragma mark - Getter
- (UICollectionView *)collectionView {
    if (!_collectionView) {
        _layout = [[HHTagCollectionViewFlowLayout alloc] init];
        
        _collectionView = [[UICollectionView alloc] initWithFrame:self.bounds collectionViewLayout:_layout];
        _collectionView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.showsHorizontalScrollIndicator = NO;
        _collectionView.showsVerticalScrollIndicator = NO;
        _collectionView.backgroundColor = [UIColor whiteColor];
        [_collectionView registerClass:[HHTagCollectionCell class] forCellWithReuseIdentifier:NSStringFromClass(HHTagCollectionCell.class)];
    }
    return _collectionView;
}

+(CGSize )intrinsicContentSizeWithTags:(NSArray<id<HHTagItemProtocol> > *)tags withConfig:(HHTagListConfig *)config {
    CGFloat contentHeight = 0;
    HHTagCollectionViewFlowLayout *layout = [[HHTagCollectionViewFlowLayout alloc] init];
    //cell的高度 = 顶部 + 高度
    contentHeight = layout.sectionInset.top + layout.itemSize.height;
    
    CGFloat originX = layout.sectionInset.left;
//    CGFloat originY = layout.sectionInset.top;
    
    return CGSizeMake(originX, contentHeight);
}
+(CGSize )intrinsicContentSize:(NSString *)text withConfig:(HHTagListConfig *)config
{
    CGSize contentSize = CGSizeZero;
    //计算文本大小
    CGFloat lineLeft = config.linePadding.left + config.contentPadding.left;
    CGFloat lineRight = config.linePadding.right + config.contentPadding.right;
    CGFloat contentMaxWidth = config.maxLayoutWidth - (config.itemPadding.left + config.itemPadding.right) - (lineLeft+lineRight);
    contentSize = [NSString bc_autoSize:text constraint:CGSizeMake(contentMaxWidth, MAXFLOAT) font:config.itemFont];
    //计算高度
    if (config.lineHeight>0) {
        //如果固定了高度
        contentSize.height = config.lineHeight;
    } else {
        //如果没有固定高度，加上 item 上下空白
        contentSize.height += config.itemPadding.top + config.itemPadding.bottom+1;
    }
    //计算宽度
    if (config.fixedItemWidth>0) {
        //如果这里是固定宽度
        contentSize.width = config.fixedItemWidth;
    } else {
        //如果没有固定宽度
        if (config.itemImg) {
            //有图片，实际宽度=图片宽度+间距+文字宽度
            contentSize.width += config.itemImg.size.width + config.itemImgTextSpace;
        }
        //加上 item 左右空白
        contentSize.width += config.itemPadding.left + config.itemPadding.right;
    }
    return contentSize;
}
@end
